package com.hnkywl.sts.service.organ;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-30
 * Time: 上午9:43
 * To change this template use File | Settings | File Templates.
 */

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.LjwhDao;
import com.hnkywl.sts.dao.xsgl.GdDao;
import com.hnkywl.sts.entity.htgl.Htdy;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.Pager;
import com.hnkywl.sts.util.QuerySearcher;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LjwhService {
    private GdDao gdDao;

    public Page<Lj> findByQuery(Pager pager, QuerySearcher queryLj) {
        return this.ljwhDao.findAll(pager, queryLj);
    }

    public List<Lj> findAll(QuerySearcher queryLj) {
        return this.ljwhDao.findAll(queryLj);
    }

    public List<Lj> findAll() {
        return this.ljwhDao.findAll();
    }

    private LjwhDao ljwhDao;

    @Autowired
    public void setLjwhDao(LjwhDao ljwhDao) {
        this.ljwhDao = ljwhDao;
    }

    @Transactional
    public void save(Lj lj) throws Exception {
        if (lj.getSy().size() > 1) {
            throw new RuntimeException("上游供应商只能有一个!");
        }
        if (lj.getXy().size() > 1) {
            throw new RuntimeException("下游代理客户只有一个!");
        }
        if (lj.getId() == null) {
            Htdy htdy = new Htdy();
            htdy.setId(99L);
            lj.setCghtmb(htdy);
            lj.setZt("ybc");
            this.add(lj);
        } else {
            this.update(lj);
        }
    }

    @Transactional
    public void add(Lj lj) {
        lj.setDateCreated(new Date());
        lj.setCreateUser(YgService.getCurrentYg());
        lj.setCreateOrgan(YgService.getCurrentYg().getZggs());
        lj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.ljwhDao.save(lj);
    }

    @Transactional
    public void update(Lj lj) {
        Lj saveDate = this.ljwhDao.get(lj.getId());
        saveDate = (Lj) ReflectionUtils.getInstance(saveDate, lj, new String[]{"deleted", "dateCreated", "lastUpdated", "createUser", "createOrgan", "createDepartment", "zt"});
        //saveDate.setSfdj(true);
        saveDate.setLastUpdated(new Date());
        //saveDate.setCreateUser(YgService.getCurrentYg());
        this.ljwhDao.save(saveDate);
    }

    @Transactional
    public void changeState(Lj lj) {
        lj.setLastUpdated(new Date());
        this.ljwhDao.save(lj);
    }

    @Transactional
    public void delete(Long id) {
        Lj lj = this.ljwhDao.get(id);
        lj.setDeleted(1);
        lj.setLastUpdated(new Date());
        this.ljwhDao.save(lj);
    }

    //启用路径
    @Transactional
    public void startLj(Long id) {
        Lj lj = ljwhDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setSfdj(false);
        this.ljwhDao.save(lj);
        //gdDao.inserToCdRz(1l, 2l, 1, 2d, 2, 2d, 2d, 2d, new Date());
    }

    //停用路径
    @Transactional
    public void stopLj(Long id) {
        Lj lj = ljwhDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setSfdj(true);
        this.ljwhDao.save(lj);
    }

    //路径可修改
    @Transactional
    public void startLjUpdate(Long id) {
        Lj lj = this.ljwhDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setKxg(true);
        this.ljwhDao.save(lj);
    }

    //路径不可修改
    @Transactional
    public void stopLjUpdate(Long id) {
        Lj lj = this.ljwhDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setKxg(false);
        this.ljwhDao.save(lj);
    }

    public String findSxy(String lx, String jyjgId, String sxy) {
        if (StringUtils.isEmpty(lx))
            lx = "0";
        if ("0".equals(lx) && StringUtils.isEmpty(sxy))
            sxy = "sy";
        else if ("1".equals(lx))
            sxy = "sy";
        else if ("2".equals(lx))
            sxy = "xy";
        else if ("3".equals(lx))
            sxy = "xy";
        else if ("5".equals(lx))
            sxy = "sy";
        List<Map> map = ljwhDao.findAllQyjg(lx, jyjgId, sxy);
        return new Gson().toJson(map);
    }

    public Lj find(Long id) {
        return this.ljwhDao.get(id);
    }

    /**
     * 查询路径明细
     *
     * @param processInstanceId 路径流程ID
     * @return
     */
    @Transactional
    public Lj findLjDetailByProcessInstance(String processInstanceId) {
        return this.ljwhDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    public Map<String, Object> bgljInfo(Long id) {
        Lj lj = ljwhDao.get(id);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("lj.id", lj != null && lj.getId() != null ? lj.getId() : "");
        data.put("jyjg.id", lj != null && lj.getJyjg() != null ? lj.getJyjg().getId() : "");
        data.put("jyjg.qc", lj != null && lj.getJyjg() != null ? lj.getJyjg().getQc() : "");
        data.put("jyjg.jc", lj != null && lj.getJyjg() != null ? lj.getJyjg().getJc() : "");
        return data;
    }

    /**
     * 根据合同定义id，查询路劲
     *
     * @param htdyId
     * @return
     */
    public List findLjByHtdyId(String htdyId) {
        String sql = "select  t.* from t_sys_lj t where t.deleted =0 and (t.xshtmb_id = ? or t.cghtmb_id = ?)";
        return this.ljwhDao.findBySQL(sql, htdyId, htdyId);
    }

    @Autowired
    public void setGdDao(GdDao gdDao) {
        this.gdDao = gdDao;
    }
}
